/*jslint vars:true plusplus:true*/
//=========================================================
// Interactive Digital Media Diploma
// CUFDIG404A_R1: Introduction to Scripting
// Assignment 3
// Author: Ben
// File: pits.js
// Description: environmental hazards for the player
//=========================================================


// A rock, pit or mine
function Hazard(x, y, sprite, collisionlayer, collisionOffset) {
    "use strict";
    
    var self = this;
    var entity = new Entity(sprite, x, y);
    
    if (collisionOffset === undefined) {
        collisionOffset = 0;
    }
    
    
    self.onHit = function(collider, layer, other){
    
        if (collisionlayer === game.environmentLayer){
            
            self.setEnabled(false);
        }
    };
    var collider = new Collider(entity, 30, collisionlayer, 0, collisionOffset, self.onHit);

    
     this.setEnabled = function(enabled){
        entity.isVisible = enabled;
        collider.isEnabled = enabled;
    };
    
    
    // shift along screen with wrap
    self.shift = function (offset) {
        entity.x += offset;
    };
    
    
    // position getter/setter
    self.getPosition = function () {
        return entity.x;
    };

    self.setPosition = function (pos) {
        entity.x = pos;
    };
}



//
// A controller for all environmental hazards
function Hazards() {
    "use strict";
    
    var self = this;
    
    // always at ground level
    var background = window.game.background;
    var yPos = background.getGroundLevel();
    
    // use the pit sprite with callback to make origin top of pit
    var pitsprite = new Sprite("images/pit.png", function (sprite) {
        sprite.offsetX = -sprite.image.width / 2;
        sprite.offsetY = 0;
    });
    
    // load rock sprite with callback to make origin at base of rock
    var rocksprite = new Sprite("images/rock.png", function (sprite) {
        sprite.offsetX = -sprite.image.width / 2;
        sprite.offsetY = -sprite.image.height;
    });
    
    // load mound sprite with callback to make origin at the base
    var moundsprite = new Sprite("images/mound.png", function (sprite) {
        sprite.offsetX = -sprite.image.width / 2;
        sprite.offsetY = -sprite.image.height;
    });
    
    // minimium time to next spawned hazard in seconds
    self.minSpawn = 1;
    
    // maximum time to next spawned hazard in seconds
    self.maxSpawn = 4;
    
    // reletive chance of spawning each hazard - alter these to
    // change the hazard mix
    self.spawnRockProbability = 0.25;
    self.spawnPitProbability = 0.5;
    self.spawnMoundProbability = 0.25;
    
    self.enabled = true;
    // all of the active hazards
    var hazards = [];
    
    // countdown timer to spawn new hazard
    var spawntimer = 0;
    
    // method to reset the spawn timer
    self.resetSpawnTimer = function () {
        spawntimer = self.minSpawn + (self.maxSpawn - self.minSpawn) * Math.random();
    };

    // method to spawn a pit
    self.spawnPit = function (x) {
        hazards.push(new Hazard(x, yPos, pitsprite, window.game.pitsLayer, 10));
    };
    
    // method to spawn a rock - always to right of screen
    self.spawnRock = function () {
        hazards.push(new Hazard(window.game.canvas.width + rocksprite.image.width, yPos, rocksprite, window.game.environmentLayer, -10));
    };


    // method to spawn a mound - always to right of screen
    self.spawnMound = function () {
        hazards.push(new Hazard(window.game.canvas.width + moundsprite.image.width, yPos, moundsprite, window.game.environmentLayer, -10));
    };

                     
    // method to spawn a random hazard on the right of the screen
    self.spawnRandomHazard = function () {
        // calculate total chance
        var totalProb = self.spawnPitProbability + self.spawnRockProbability + self.spawnMoundProbability;
        var randomVal;
        // if there is a chance of spawning
        if (totalProb > 0) {
            randomVal = totalProb * Math.random();
            
            // check for spawn pit
            if (randomVal < self.spawnPitProbability) {
                self.spawnPit(window.game.canvas.width + pitsprite.image.width);
            } else {
                // check for spawn mound
                randomVal -= self.spawnPitProbability;
                if (randomVal < self.spawnMoundProbability) {
                    self.spawnMound();
                } else {
                    // check for spawn rock
                    randomVal -= self.spawnMoundProbability;
                    if (randomVal < self.spawnRockProbability) {
                        self.spawnRock();
                    }
                }
            }
                    
        }
    };
    
    
    // update method called by game class
    self.update = function () {
        var hazardIdx;
        var hazard;
        if (self.enabled) {
            
            // count down the timer by a fraction of a second
            spawntimer -= 1 / window.game.frameRate;

            // if the timer has ended
            if (spawntimer < 0) {
                // spawn new hazard
                self.spawnRandomHazard();

                // reset the timer
                self.resetSpawnTimer();
            }
        }
        // shift all hazards at base speed. 
        for (hazardIdx = 0; hazardIdx < hazards.length; hazardIdx++) {
            hazard = hazards[hazardIdx];
            
            hazard.shift(-background.getBaseSpeed() / window.game.frameRate);
            
        }
        
        // remove any offscreen hazards
        hazards = hazards.filter(function (hazard) { return hazard.getPosition(); });
        
    };
    
}
window.game.hazards = new Hazards();
window.game.addSubsystem(window.game.hazards);